<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-row :gutter="15">
        <el-col :span="6">
          <el-form-item label="评估机构" prop="orgName">
            <el-input v-model="form.evaluateAgency" placeholder="请输入评估机构"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="评估地点" prop="address">
            <el-input v-model="form.evaluatePlace" placeholder="请输入评估地点"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否到场" prop="flag">
            <el-select v-model="form.evaluateIsPresent" placeholder="请选择是否到场">
              <el-option label="是" :value="1"/>
              <el-option label="否" :value="0"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="评估时间" prop="dotime">
            <el-date-picker
              clearable
              v-model="form.evaluateTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择评估时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col>
          <el-form-item label="评估附件" prop="evaluateAttachment">
            <file-upload v-if="!isShow" :fileSize="20" ref="fileUpload"/>
            <file-show v-if="isShow" v-model="form.oss" @refresh="refresh" ref="fileUpload"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :offset="2" :span="22">
          <el-card shadow="hover">
            <div slot="header" class="clearfix">
              <span>评估清单</span>
              <el-button type="text" style="float:right;padding: 3px 0" @click="addDtl">新增</el-button>
            </div>
            <el-table :data="form.evaluateProList" border stripe>
              <el-table-column label="序号" type="index" width="60" align="center"/>
              <el-table-column label="物品编号" align="center" prop="proNo"/>
              <el-table-column label="物品名称" align="center" prop="proName"/>
              <el-table-column label="评估数量" align="center" prop="evaluateNumber">
                <template slot-scope="scope">
                  <el-input-number :controls="false" placeholder="请输入评估数量" v-model="scope.row.evaluateNumber"/>
                </template>
              </el-table-column>
              <el-table-column label="评估价值" align="center" prop="evaluateValue">
                <template slot-scope="scope">
                  <el-input-number :controls="false" placeholder="请输入评估价值" v-model="scope.row.evaluateValue"/>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <div>
                    <span v-if="scope.row.delFlag =='1'">已删除</span>
                    <el-button v-else type="danger" size="mini" @click="dtlDel(scope)">删除</el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>
      </el-row>
    </el-form>
    <select-property ref="elDrawer" @handleSelected="handleSelected"/>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addEvaluate, getEvaluate, updateEvaluate } from '@/api/sacw/evaluate'
import SelectProperty from '@/components/SelectProperty'
import FileShow from '@/components/FileShow'

export default {
  name: 'evaluateForm',
  components: { FileShow, SelectProperty },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      isShow: false
    }
  },
  computed: {
    // 表单校验
    rules() {
      return {
        evaluateAgency: [{
          required: true,
          message: '评估机构不能为空',
          trigger: 'blur'
        }],
        evaluateTime: [{
          required: true,
          message: '评估时间不能为空',
          trigger: 'blur'
        }],
        evaluatePlace: [{
          required: true,
          message: '评估地点不能为空',
          trigger: 'blur'
        }],
        evaluateIsPresent: [{
          required: true,
          message: '是否到场不能为空',
          trigger: 'change'
        }]
      }
    }
  },
  methods: {
    getList() {
      this.$emit('getList')
    },
    addDtl() {
      this.$refs.elDrawer.open()
    },
    dtlDel(scope) {
      let index = scope.$index
      if (scope.row.id) {
        this.$set(scope.row, 'delFlag', '1')
      } else {
        this.form.evaluateProList.splice(index, 1)
      }
    },
    //获取选择后的财务数据
    handleSelected(array) {
      let data = array.map(item => {
        return {
          proNo: item.pno,
          proName: item.name,
          evaluateNumber: item.nums
        }
      })
      if (!!!this.form.evaluateProList) {
        this.$set(this.form, 'evaluateProList', [])
      }
      this.form.evaluateProList.push(...data)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.isShow = false
      this.title = '添加评估'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getEvaluate(row.id).then(response => {
        this.form = response.data
        this.open = true
        this.isShow = true
        this.title = '修改评估'
      })
    },
    refresh(){
      getEvaluate(this.form.id).then(response => {
        this.form = response.data
      })
    },
    /** 提交按钮 */
    submitForm() {
      if(!this.isShow){
        this.form.oss = this.$refs.fileUpload.pics
      }
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateEvaluate(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.reset()
              this.open = false
              this.getList()
            }).finally(() => {
              this.buttonLoading = false
            })
          } else {
            addEvaluate(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.reset()
              this.open = false
              this.getList()
            }).finally(() => {
              this.buttonLoading = false
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
